package oa.dao.impl;

import java.util.List;

import oa.base.BaseDao;
import oa.dao.DepartmentDao;
import oa.entity.Department;
import oa.exception.DaoException;

public class DepartmentDaoImpl extends BaseDao<Department> implements DepartmentDao {

	@Override
	public boolean addDepartment(Department department) throws DaoException {
		if(department.getDeptPrincipal()==0) {
			return executeUpdate(
					"INSERT INTO `oa`.`department` (`dept_name`, `dept_phone`, `dept_principal`, `dept_blurb`, `company_id`) VALUES (?, ?, ?, ?, ?)",
					department.getDeptName(), department.getDeptPhone(),department.getDeptPrincipal(), department.getDeptBlurb(),
					department.getCompanyId()) > 0;
		}else {
		
		
		return executeUpdate(
				"INSERT INTO `oa`.`department` (`dept_name`, `dept_phone`, `dept_principal`, `dept_blurb`, `company_id`) VALUES (?, ?, ?, ?, ?)",
				department.getDeptName(), department.getDeptPhone(),department.getDeptPrincipal(), department.getDeptBlurb(),
				department.getCompanyId()) > 0;
	}           
	}

	@Override
	public List<Department> selectAllDepartment(int companyid,Integer pageNo, Integer row) throws DaoException {
		if(pageNo == 0 && row == 0) {
			return executeQuery("SELECT * FROM department ");
		}else if(companyid!=0){
			return executeQuery("SELECT * FROM department  WHERE  company_id = ? LIMIT ?,?",companyid,(pageNo-1)*row,row);
		}else{
			return executeQuery("SELECT * FROM department  LIMIT ?,?",(pageNo-1)*row,row);
		}
	}

	@Override         
	public List<Department> selectDepartment(int departmentId, String departmentName, int companyId)
			throws DaoException {
	
		if (departmentId !=0) {
			return executeQuery("SELECT * FROM department WHERE dept_id = ?", departmentId);
			
		} else if (departmentName != null) {
			return executeQuery("SELECT * FROM department WHERE dept_name = ?", departmentName);
			
		} else if(departmentId==0 && departmentName==null && companyId ==0 ) {
			return executeQuery("SELECT * FROM department ");
			
		}else {
			return executeQuery("SELECT * FROM department WHERE company_id = ?",companyId);
			
		}
		
	}

	@Override
	public boolean updateDepartment(Department department) throws DaoException {
		return executeUpdate(
				"UPDATE `oa`.`department` SET `dept_name`=?, `dept_phone`=?,`dept_principal`=?, `dept_blurb`=?, `company_id`=? WHERE (`dept_id`=?)",
				department.getDeptName(), department.getDeptPhone(),department.getDeptPrincipal(), department.getDeptBlurb(),
				department.getCompanyId(), department.getDeptId()) > 0;
	}

	@Override
	public boolean deleteCompany(int departmentId) throws DaoException {
		return executeUpdate("DELETE FROM `department` WHERE (`dept_id`=?)", departmentId) > 0;
	}

	@Override
	public int countDepartment(int departmentId)throws DaoException {
		String sql = null;
		Object obj = null;
		if(departmentId>0) {
			sql=" SELECT COUNT(*) FROM department WHERE company_id =?";
			obj= departmentId;
			return executeIntQuery(sql, obj);
		}else {
			sql = "SELECT COUNT(*) FROM department";
			return executeIntQuery(sql);
		}
	}

}
